package com.tensquare.friend.controller;

import com.tensquare.friend.client.FriendClient;
import com.tensquare.friend.service.FriendService;
import com.tensquare.friend.service.NoFriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private NoFriendService noFriendService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private FriendClient friendClient;
    //1.添加关注
    @PutMapping("like/{friendid}/{type}")
    public Result like(@PathVariable String friendid,@PathVariable String type){
        //1.1从request作用域中得到claims
        Claims claims = (Claims) request.getAttribute("claims");
        //1.2判断是否有值以及roles值是否为user,为user则说明登录了,且是用户登录
        if (claims!=null && "user".equals(claims.get("roles"))){
            //1.3判断是否为1 , 为1的话说明是添加对好友的关注 , 否则添加的是对非好友的关注
            if ("1".equals(type)){
                //添加对好友的关注(返回false则说明之前添加过)
                if (friendService.add(claims.getId(),friendid)){
                    //给当前对象的关注数加一 , 其关注的对象粉丝数加一
                    friendClient.updateFollow(claims.getId(),1);
                    friendClient.updateFans(friendid,1);
                }else {
                    //之前添加过
                    return new Result(false,StatusCode.REPERROR,"不能重复添加关注");
                }
            }else {
                //添加对非好友的关注
                if (noFriendService.add(claims.getId(),friendid)){
                    friendClient.updateFollow(claims.getId(),1);
                    friendClient.updateFans(friendid,1);
                }else {
                    //之前添加过
                    return new Result(false,StatusCode.REPERROR,"不能重复添加关注");
                }
            }
            return new Result(true,StatusCode.OK,"添加关注成功");
        }else {
            return new Result(false, StatusCode.LOGINERROR,"您无权执行此操作");
        }
    }
    //2.取消关注
    @DeleteMapping("unlike/{friendid}/{type}")
    public Result unlike(@PathVariable String friendid,@PathVariable String type){
        //2.1从request作用域中得到claims
        Claims claims = (Claims) request.getAttribute("claims");
        //2.2判断是否有值以及roles值是否为user,为user则说明登录了,且是用户登录
        if (claims!=null && "user".equals(claims.get("roles"))){
            //2.1判断type值 , 为1的话说明是好友,删除好友的关注表 , 否则删除非好友的关注表
            if ("1".equals(type)){
                if (friendService.delete(claims.getId(),friendid)){
                    //给当前对象的关注数-1 , 其关注的对象粉丝数-1
                    friendClient.updateFollow(claims.getId(),-1);
                    friendClient.updateFans(friendid,-1);
                    return new Result(true,StatusCode.OK,"取消对好友的关注成功");
                }
            }else {
                if (noFriendService.delete(claims.getId(),friendid)){
                    //给当前对象的关注数-1 , 其关注的对象粉丝数-1
                    friendClient.updateFollow(claims.getId(),-1);
                    friendClient.updateFans(friendid,-1);
                    return new Result(true,StatusCode.OK,"取消对非好友的关注成功");
                }
            }
        }else {
            return new Result(false,StatusCode.ERROR,"您无权执行此操作");
        }
        return new Result(false,StatusCode.REPERROR,"您未关注此用户");
    }
}
